package cn.skyclass.utils
{
	import mx.collections.ArrayCollection;
	
	public class StringUtil
	{
		/**
		 * 将集合数据连接成字符串
		 * eg: join(new ArrayCollection([1, 2, 3]), ",");
		 *     --> 1,2,3
		 */
		public static function join(values:ArrayCollection, separator:String=","):String{
			var out:String = "";
			for each(var o:Object in values){
				out += separator + o.toString();
			}
			if(out){
				out = out.substring(separator.length);
			}
			return out;
		}
		
		/**
		 * 将一个字符串分割成 int 集合
		 */
		public static function splitToInt(str:String, separator:String=","):ArrayCollection{
			var out:ArrayCollection = new ArrayCollection();
			if(str){
				var arr:Array = str.split(separator);
				for each(var o:Object in arr){
					out.addItem(int(o));
				}
			}
			return out;
		}
		
		
		/**
		 * 获取 bool 值对应的中文字符
		 * eg: true --> 是；false --> 否
		 */
		public static function getBoolString(value:Boolean):String{
			return value ? "是" : "否";
		}
		
		/**
		 * 根据字符串获取 bool 值，'true','false'字符串返回相应的bool值，
		 * 非 0 的数字字符串返回 true，其余字符串返回 false
		 */
		public static function getBoolValue(str:String):Boolean{
			if(!str || str.toLowerCase() == "false"){ return false; }
			if(str.toLowerCase() == "true"){ return true; }
			var i:int = int(str);			
			return (i != 0);
		}		
		
		/**
		 * 获取字符串需要的显示长度
		 * @param str 目标字符串
		 * @param enCharLen 显示一个英文字符需要的长度，默认 6 个像素
		 * @param cnCharLen 显示一个中文字符需要的长度，默认 12 个像素
		 * @return 显示长度（像素数）
		 */		
		public static function getUiLength(str:String, enCharLen:int=6, cnCharLen:int=12):int{
			if(!str){ return 0; }
			
			var pickedArr:Array = 
				str.match(/\w+|\s+|[\~\!\@\#\$\%\^\&\*\(\)\_\+\`\-\=\[\]\{\}\;\'\:\"\,\.\/\<\>\?]+/g);
			var outStr:String = pickedArr.join("");
			
			var enCharCount:int = outStr ? outStr.length : 0;
			var cnCharCount:int = str.length - enCharCount;
			var totalLen:int = enCharCount * enCharLen + cnCharCount * cnCharLen;
			return totalLen;
		}		

		/**
		 * 字符串中是否含有中文字符
		 */
		public static function containsChineseChar(str:String):Boolean{
			if(!str){ return false; }
			
			var pickedArr:Array = 
				str.match(/\w+|\s+|[\~\!\@\#\$\%\^\&\*\(\)\_\+\`\-\=\[\]\{\}\;\'\:\"\,\.\/\<\>\?]+/g);
			var outStr:String = pickedArr.join("");
			return (str != outStr);
		}	
		
		/**
		 * 判断给定的字符串中是否只含有正则表达式所定义的字符
		 */
		public static function onlyContainsChar(str:String, pattern:RegExp):Boolean{
			if(!str){ return false; }
			
			var pickedArr:Array = str.match(pattern);
			var outStr:String = pickedArr.join("");
			return (str == outStr);
		}		
				
	}
}